//
//  CalendarHomeViewController.m
//  Calendar
//
//  Created by 张凡 on 14-6-23.
//  Copyright (c) 2014年 张凡. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "CalendarHomeViewController.h"
#import "Color.h"
#import "CalendarDayModel.h"

@interface CalendarHomeViewController ()
{

    
    int daynumber;//天数
    int optiondaynumber;//选择日期数量
//    NSMutableArray *optiondayarray;//存放选择好的日期对象数组
    
}

@end

@implementation CalendarHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
//        //往后添加指定时间天数
//        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
//        [but setFrame:CGRectMake(self.view.frame.size.width-80, 50, 50, 50)];
//        [but setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:1 alpha:0.45]];
//        [but setTitle:@"+" forState:UIControlStateNormal];
//        [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [but.layer setMasksToBounds:YES];
//        [but.layer setCornerRadius:but.frame.size.width/2];
//        
//        [but addTarget:self action:@selector(addmoths:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view  insertSubview:but atIndex:9999];
        
        
//        //回到今天
//        UIButton *butToday=[UIButton buttonWithType:UIButtonTypeCustom];
//        [butToday setFrame:CGRectMake(30, 50, 50, 50)];
//        [butToday setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:1 alpha:0.45]];
//        [butToday setTitle:@"Day" forState:UIControlStateNormal];
//        [butToday setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [butToday.layer setMasksToBounds:YES];
//        [butToday.layer setCornerRadius:butToday.frame.size.width/2];
//        
//        [butToday addTarget:self action:@selector(today:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view  insertSubview:butToday atIndex:9998];
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    

	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//往后添加指定月份
-(void)addmoths:(UIButton*)sender{
 
    NSArray *arrMoths= [self getADDMOTHS];
    //因为之前会多算一个月的 否咋会出现重复
    [super.calendarMonth removeObjectAtIndex:super.calendarMonth.count-1];
    [super.calendarMonth addObjectsFromArray:arrMoths];
    
    [super.collectionView reloadData];
}
//回到今天
-(void)today:(UIButton*)sender{
    
  [super.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:super.pushforwardMoths] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    
}
#pragma mark - 设置方法


/**
 *  初始化方法
 *
 *  @param day    可选时间跨度
 *  @param todate 默认选中时间
 */
- (void)setHotelToDay:(int)day ToDateforString:(NSString *)todate
{

    daynumber = day;
    optiondaynumber = 1;//选择两个后返回数据对象
    
  super.calendarMonth = [self getMonthArrayOfDayNumber:daynumber ToDateforString:todate];
    
    NSInteger section=[self getSection:todate];
    
 //滑动到某个位置
  [super.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
 
 
}

/**
 *  返回当前选择天所在项
 *
 *  @param todate <#todate description#>
 *
 *  @return  
 */
-(NSInteger)getSection:(NSString *)todate{
    NSInteger setcion=0;
    NSDate *selectdate  = [UtilCheck dateByString:todate oldFormatter:@"yyyy-MM-dd" dateFormatter:@"yyyy-MM-dd"];
 
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: selectdate];
   selectdate=[selectdate  dateByAddingTimeInterval: interval];
    
   NSDateComponents *todayDC =  [[NSDate date] YMDComponents];
      NSDateComponents *selectdayDC =  [selectdate YMDComponents];
     NSInteger todayMonth = todayDC.month;
     NSInteger selectdayMonth = selectdayDC.month;
      setcion=selectdayMonth-todayMonth+1;
 
    return setcion;
}


#pragma mark - 逻辑代码初始化
/**
 *  获取时间段内的天数数组
 *
 *  @param day    多少天可选
 *  @param todate 今天
 *
 *  @return 获取时间段内的天数数组
 */
- (NSMutableArray *)getADDMOTHS
{
      NSDate *date=[NSDate date];
    
  //找到上期最后一天
   NSArray *arr= [super.calendarMonth objectAtIndex:super.calendarMonth.count-1];
 
    for(int i=0;i<arr.count;i++){
        CalendarDayModel *days=arr[i];
        if(days.style==CellDayTypePast){
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"YYYY-MM-dd"];
            date = [formatter dateFromString:days.toString];
            break;
        }
        
    }
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate: date];
//    date = [date  dateByAddingTimeInterval: interval];
    
    if( !super.Logic){
    super.Logic = [[CalendarLogic alloc]init];
    }
    
    return [super.Logic addReloadCalendarView:date   needDays:daynumber];
}


/**
 *  获取时间段内的天数数组
 *
 *  @param day    多少天可选
 *  @param todate 今天
 *
 *  @return 获取时间段内的天数数组
 */
- (NSMutableArray *)getMonthArrayOfDayNumber:(int)day ToDateforString:(NSString *)todate
{
    
    NSDate *date = [NSDate date];
    
    NSDate *selectdate  = [UtilCheck dateByString:todate oldFormatter:@"yyyy-MM-dd" dateFormatter:@"yyyy-MM-dd"];
 
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: selectdate];
     date = [date  dateByAddingTimeInterval: interval];
    selectdate=[selectdate  dateByAddingTimeInterval: interval];
    
//    if (todate) {
//        
//        selectdate = [selectdate dateFromString:todate];
//        
//    }
    
    super.Logic = [[CalendarLogic alloc]init];
    super.Logic.pushforwardMoths=self.pushforwardMoths;
    super.Logic.pfMothsCanSelect=self.pfMothsCanSelect;
    return [super.Logic reloadCalendarView:date selectDate:selectdate  needDays:day];
}



#pragma mark - 设置标题

- (void)setCalendartitle:(NSString *)calendartitle
{

    [self.navigationItem setTitle:calendartitle];

}


@end
